package com.zrc.superframe_base.mvx.mvc

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.zrc.superframe_base.mvx.base.BaseFragment
import com.zrc.superframe_net.interfac.NetBase
import io.reactivex.disposables.Disposable

/**
 * @Author: superman
 * @CreateTime: 2020/7/4
 * @Describe: BaseFragment基类
 */
abstract class BaseMvcFragment : BaseFragment() , NetBase {
    //请求队列（界面销毁时需要回收的）
    private var requestQueue= ArrayList<Disposable>()
    private var mView: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView=inflater.inflate(getLayoutID(), container, false)
        return mView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView(mView)
        initListener()
        initData()
    }

    protected abstract fun getLayoutID(): Int

    open fun initView(contentView: View?) {}

    open fun initData() {}

    open fun initListener() {}

    override fun onDestroy() {
        super.onDestroy()
        netGc()
    }

    override fun addRequestQueue(d: Disposable)=if (requestQueue .contains(d)) {
        requestQueue.remove(d)
        false
    }else{
        requestQueue.add(d)
        true
    }

    override fun removeRequestQueue(d: Disposable) {
        requestQueue.remove(d)
    }

    override fun netGc() {
        requestQueue.forEach { //取消队列
            if (!it.isDisposed) {
                it.dispose()
            }
        }
        requestQueue.clear()
    }
}